package com.graduate.after.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝接口配置类
 */
@Configuration
public class PayConfig {

    /**
     *  应用id  后期可以替换成自己的id
     */
    private static final String appID = "2016102700769671";

    /**
     *用户私钥  后期替换成自己的私钥
     */
    private static final String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCiItVMIbo3dHMFJqUiIjPH" +
            "6fUPXjBbMsTZUPufN6TqnAWJoJeD09YTbOm0DpF/kvKI9hAZYdVFfdOoY9UZTqaO3+THWqC/v5ZAxZrjVVYDEBQg5a5cIESMFhdY2HN5S" +
            "KLe628WLgiVaQZIin94XkDZM7OTqh0COt91xunoz5wChZDISvlPHT6qg5TdCMrSO/4GdcYQSxPLZcYg2Lwv//wUSvvOhtbTBC/pkXO+0M" +
            "Prh9o4pR3DrXz3+eDFb74HJjAS54AdbJqbB02dgUM1E4N4GZwTEb4zUwbqZ54RC2h/peUyC2T6xdjPR6+knTXyq8HjDbGO/uvClFvma65" +
            "6zyh/AgMBAAECggEAGkGT0xlfI53bBomEkmuBgS1U/yEcqZgmIp8nsFf9f/S2PNxhzPpgH5MyoXL+gu9UEXPvlXyvYeqHfSx1lPyUomyF" +
            "ivcVMxgeh+y4MXpfTG2tJMqtGsQq9ZiaPUwIHyrxHTJ5MdlQkImKd/Ldw6tXyxTBTvdsbTEFgPguZwI/3HRsaDAosLpb3KpDIDK/1XeCc" +
            "ZVwK9XQpbMqV83oMfgiF4GK/xsR14MZxq2hRRo81oSfwakBbZ735KBmyDzV7cNsmLyTav2gbsSri/Ec53Nl31WCzHwcAzCkF1uMGrXsei" +
            "OsL9EAZ8EsLwh1ISpPCuqmLz6NV76crJxwV7Ee3YnimQKBgQDi7+hbxgJKgA3Veqr+vKqzK9OjzOqsoCucE0o3iPAu23wZFi9TlWe2sn3" +
            "LCYRN7sM08uOJWoUk4gUUjnSjnpSnatcOSyrBOmkiJRggijrIEx68B7HNJGyYf+2SW4n+bqxkm7+CkQNwJgyCOzwwtrgtRqvbu6QF45Q9" +
            "Aa/QRAUUewKBgQC25m8vfyRYzLJ9f2lZvmimap3YnTRxNw0awsAWa8N0wOKzhecJOIXjklFvYNA8wNbsR2b0IiJ+i4RC/z7hUqhkZfGEb" +
            "elKFkX5eePqt2HnAyibJ2fwHDk1g3/S33OvSwf3nT0kE80SbBeYLo9/TpSi6tr1ZK/2wCuvWmkqo4KmzQKBgQCiRGMEBDCtLqU1qVHNdg" +
            "Akv0jEjgibSKgGILDnxs3e42f31Cog/Oq88yIJZROqWfhQywAxvX0Io2h4i2gRepnWoAmIGe1HX0m/dYCr+8sONsW6M1JKWiogXRD1nu" +
            "ir+lzgT5F/fctUjJBTKqHePziwfyDxrFkbMOBVQviA1bJAMwKBgQCgXgYJOEqvTbQHE8tUs/5gREVoo9ZSmnl0CGSShCSZ33InBHZiAFx" +
            "tAGNE7DdJva/09lpQHwntuPjcIZ+Jw4cAmN4eRpzxp/vs1bJAoFBIVN2qxO9+edCzmjxesixl+7zdoNH2KyJ5T6uUdOa/MjBjdNdNqN4c" +
            "1by4AOcLStkI1QKBgQCApoNZ6ef35D2tz6RlE81lhzBQX75r9OlpfzX/tvd06PsiVwZyESNBnrJemvJQbi78YeUynwblxDqvXkql+soTV" +
            "oY+kmUm2jnbAujmMgUyxR2RzRZRQL2SkUp41fTxaoVPx4NWobD9uqIybbkes8WTm8mJ7NxPM3id3PWhKQvDTg==";

    /**
     *  支付宝公钥 后期替换成自己的支付宝公钥
     */
    public static final String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmcUuExy0G1A9RtC6ph375s9dNhJszn" +
            "6RrMRanfZonmuw+zm+6aCQAqEL3rYHqLTdUsxc7Er4Thy7p0rJK2JOrZ9Y6vEMjrdKP//o6CE6xerMxj104KcvdYIfAHYK/UrrCiyYI5g" +
            "Y+fjdLDx8U+sHMtTDRlpY1izPS9q3eOZk8C+n3CaclyvbUqEQQPejPaTt9e77Wr8lXkVKvN9Yq9BNPhQjeDHZg6CYU62NWOM/2S5MUFBr" +
            "daUxZMQ3fxqcwpAZ+r7M7CyLSjKpNzgSWAxBHl3peyV3arUrTmZdT6JUhsW2HiouAZXh+i9DhF7ir02s6s/l7B5BkeVt6AhSxJyE2QIDAQAB";

    /**
     * 支付宝签名 目前是 RSA2
     */
    public static final String signType = "RSA2";


    /**
     *  设置字符集编码 目前只能为utf-8
     */
    public static String charset = "UTF-8";

    /**
     * 服务网关  沙箱环境都是这个
     */
    public static String serverUrl = "https://openapi.alipaydev.com/gateway.do";

    /**
     * 发送数据的格式 目前只能为json
     */
    public static String format = "json";

    @Bean
    public AlipayClient alipayClient(){
        //沙箱环境使用https://openapi.alipaydev.com/gateway.do，线上环境使用https://openapi.alipay.com/gateway.do
        return new DefaultAlipayClient(serverUrl, appID, privateKey, format, charset, publicKey, signType);
    }

    /**
     * 验签，是否正确
     */
    public static boolean checkSign(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, String> paramsMap = new HashMap<>();
        requestMap.forEach((key, values) -> {
            String strs = "";
            for(String value : values) {
                strs = strs + value;
            }
            System.out.println(key +"===>"+strs);
            paramsMap.put(key, strs);
        });
        //调用SDK验证签名
        try {
            return  AlipaySignature.rsaCheckV1(paramsMap, PayConfig.publicKey, PayConfig.charset, PayConfig.signType);
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("*********************验签失败********************");
            return false;
        }
    }

}